/***************************************************************************
 *   Copyright (C) 2007 by Grzegorz Latarowski, Maciej Gajewski            *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/




// local
#include "gnetpackets.h"




// ===========================================================================
//                            GNetPacketFactory
// ===========================================================================


// instance declaration
GNetPacketFactory * GNetPacketFactory::_pPacketFactoryInstance = NULL;


// ---------------- GNetPacketFactory::instance() ----------------------------
GNetPacketFactory * GNetPacketFactory::instance()
{
	if ( ! _pPacketFactoryInstance )
	{
		_pPacketFactoryInstance = new GNetPacketFactory();

			// register default GNetPackets 
		_pPacketFactoryInstance->registerPacket(
				GNetPacket::GNETPACKET_AUTHORIZATION,
				&(GNetPacketAuthorization::createPacket)
											   );
		_pPacketFactoryInstance->registerPacket(
				GNetPacket::GNETPACKET_AUTHORIZATION_ANSWER,
				&(GNetPacketAuthorizationAnswer::createPacket)
											   );
		_pPacketFactoryInstance->registerPacket(
				GNetPacket::GNETPACKET_UNKNOWN,
				&(GNetPacketUnknown::createPacket)
											   );
		
	}

	return _pPacketFactoryInstance;
}
// ---------------------------------------------------------------------------




// ===========================================================================
//                                 GNetPacket
// ===========================================================================


// -------------------------- GNetPacket::header() ---------------------------
/// Creates approproate header using packet type info
GHeader GNetPacket::header() const
{
	GHeader header;
	header.setType( packetType() );

	return header;
}
// ---------------------------------------------------------------------------




// ===========================================================================
//                            GNetPacketAuthorization
// ===========================================================================


// ---------------------- GNetPacketAuthorization::write() -------------------
void GNetPacketAuthorization::writeData( QDataStream & out ) const
{
	out << _login;
	out << _password;
}
// ---------------------------------------------------------------------------


// ---------------------- GNetPacketAuthorization::read() --------------------
void GNetPacketAuthorization::readData( QDataStream & in )
{
	in >> _login;
	in >> _password;
}
// ---------------------------------------------------------------------------




// ===========================================================================
//                         GNetPacketAuthorizationAnswer
// ===========================================================================


// ------------------ GNetPacketAuthorizationAnswer::write() -----------------
void GNetPacketAuthorizationAnswer::writeData( QDataStream & out ) const
{
	out << (quint16)_status;
	out << _message;
}
// ---------------------------------------------------------------------------


// ------------------ GNetPacketAuthorizationAnswer::read() ------------------
void GNetPacketAuthorizationAnswer::readData( QDataStream & in )
{
	quint16 status;
	
	in >> status;
	_status = (Status)status;
	in >> _message;
}
// ---------------------------------------------------------------------------



